package com.gralves.elasticsearch.utils.config;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

/**
 * @author zhouyibin
 * @date 2018/12/17
 * @desc
 */
@Slf4j
@Configuration
public class EsFactory {
    @Value("${elasticsearch.nodes}")
    private String nodes = "10.0.99.128";
    private static final Integer port = 9300;
    @Value("${elasticsearch.clusterName}")
    private String clusterName = "bulk";
    @Value("${elasticsearch.nodeName}")
    private String nodeName = "bulk-01";

    @Bean
    public TransportClient client() {
        log.warn(">>>>>>>>>>>>>>>>>>[ initial elasticSearch client ... ]<<<<<<<<<<<<<<<<");
        Settings settings = Settings
                .builder()
                .put("cluster.name", clusterName)
                .put("node.name", nodeName)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        for (String node : nodes.split(",")) {
            TransportAddress address = new TransportAddress(new InetSocketAddress(node, port));
            client.addTransportAddress(address);
        }
        log.warn(">>>>>>>>>>>>>>>>>>[ elasticsearch : version ==> 6.3.2 ]<<<<<<<<<<<<<<<<");
        log.warn(">>>>>>>>>>>>>>>>>>[ finish initial elasticSearch client ...]<<<<<<<<<<<<<<<<");
        return client;
    }
}
